#include "StdAfx.h"
#include "ServerShipsLaunchedMessage.h"
#include "FlightInfo.h"
#include "GameStateAccessor.h"
#include <boost/foreach.hpp>
#include "MessageBuilder.h"

ServerShipsLaunchedMessage::ServerShipsLaunchedMessage(GalconRoom& galconRoom, const FlightInfoPtr flightInfoPtr
                                                      , const ServerParticipantPtr serverParticipant)
   : m_flightInfoPtr(flightInfoPtr)
   , m_galconRoom(galconRoom)
   , m_participant(serverParticipant)
{
}


ServerShipsLaunchedMessage::~ServerShipsLaunchedMessage(void)
{
}

void ServerShipsLaunchedMessage::do_execute()
{
   const PlanetIDMap& planets = m_flightInfoPtr->GetSrcPlanets();
   GameStateAccessorPtr m_gameStateAccessor = m_galconRoom.GetGameStateAccessor();
   BOOST_FOREACH(const PlanetShipsCountPair& currentPlanet, planets)
   {
      double countOfShips = m_gameStateAccessor->GetShipCount(currentPlanet.first);
      m_gameStateAccessor->SetShipCount(currentPlanet.first, countOfShips - currentPlanet.second);
   }
   MessagePtr messagePtr = MessageBuilder::BuildShipsLaunchedMessage(m_flightInfoPtr);
   m_galconRoom.Deliver(*messagePtr, m_participant);
   m_galconRoom.Deliver(*m_gameStateAccessor->GetGameStateMsg());
}